package com.jcw.kqxt.Interceptor;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.jcw.kqxt.constants.Constant;
import com.jcw.kqxt.customAnnotation.PermissionVerification;
import com.jcw.kqxt.exception.MyException;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//自定义注解
@Component
public class LoginInterceptor implements HandlerInterceptor {
//    //false 拦截 true放行
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        //1.校验token存在
//        String token = request.getHeader(Constant.TOKEN);
//        if(token==null){
//            throw new MyException("未登录,请去登录!");
//        }
//        JWT jwt = JWTUtil.parseToken(token);
//        //检验过期token
//        long audience = Long.valueOf(jwt.getPayload().getClaim(Constant.ISSUEDAT).toString());
//        //超时强退
//        if(audience+72*60*60*1000-System.currentTimeMillis()<=0){
//            throw new MyException(Constant.OVERTIME);
//        }
//        //刷新token
//        if(audience+72*60*60*1000-System.currentTimeMillis()<=0){
//            throw new MyException(Constant.OVERDUE);
//        }
//
//        if(!(handler instanceof HandlerMethod)){
//            return true;
//        }
//        HandlerMethod handlerMethod = (HandlerMethod) handler;
//        PermissionVerification methodAnnotation = handlerMethod.getMethodAnnotation(PermissionVerification.class);
//        if(methodAnnotation==null){
//            return true;
//        }
//        Object claim = jwt.getPayload().getClaim(Constant.AUTHLIST);
//        if(claim==null){
//            throw new MyException("没有权限,请联系管理员!");
//        }
//        List<String> list = (List<String>) claim;
//        String annotationValue = methodAnnotation.auth();
//        if(!list.contains(annotationValue)){
//            throw new MyException("没有权限,请联系管理员!");
//        }
//        return true;
//    }
}